package com.hishop.product.controller;

import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.R;
import com.hishop.product.dao.AttrDao;
import com.hishop.product.entity.AttrAttrgroupRelationEntity;
import com.hishop.product.entity.AttrEntity;
import com.hishop.product.entity.AttrGroupEntity;
import com.hishop.product.service.AttrAttrgroupRelationService;
import com.hishop.product.service.AttrGroupService;
import com.hishop.product.service.CategoryService;
import com.hishop.product.service.impl.AttrServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author tanwenbin
 * @email 2466082499@qq.com
 * @date 2024-03-13 09:56:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrServiceImpl attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Long catId){
        // 为 0 就是查询全部的，为 1 查询单独的
        System.out.println("参数Id的值：" + catId);
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 属性分组的关联
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R attrAttrGroupRelationList(@PathVariable("attrGroupId") Long attrGroupId){
        // 根据 分组id 获取到关系行数据
        Long[] longs = attrAttrgroupRelationService.selectAttrId(attrGroupId);
        List<AttrEntity> data = new ArrayList<>();
        for (int i = 0; i < longs.length; i++) {
            System.out.println("attrid:" + longs[i]);
            AttrEntity attr1 = attrDao.selectById(longs[i]);
            data.add(attr1);
        }
        return R.ok().put("data", data);
    }

    /**
     * 属性分组的新增关联列表
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R addAttrRelationList(@RequestParam Map<String, Object> params,
                                 @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.queryPage1(params,attrGroupId);
        List<PageUtils> data = new ArrayList<>();
        data.add(page);
        return R.ok().put("page", page);
    }

    /**
     * 新增关联属性【修改关联表的分组id】
     * @param postData
     * @return
     */
    @RequestMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R saveAddAttrAttrGroup(@RequestBody List<AttrAttrgroupRelationEntity> postData){
        // 属性、分组表
        List<AttrAttrgroupRelationEntity> data = new ArrayList<>();
        /**
         * 使用 for 循环获取 attrId、attrGroupId【可能会有多个】，修改 关联表的attrId 对应的 attrGroupId
         */
        for (AttrAttrgroupRelationEntity attrAttrgroupRelation : postData) {
            // 获取 分组id 、 属性id
            Long attrGroupId = attrAttrgroupRelation.getAttrGroupId();
            Long attrId = attrAttrgroupRelation.getAttrId();

            // 重新设置 属性表的 相关分组id【数据库没有对应的列，不会写进 数据库中】
            AttrEntity attr = attrDao.selectById(attrId);
            attr.setAttrGroupId(attrGroupId);// 重新设置分类id

            // 根据属性Id获取 属性和分组实体
            AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.selectByAttrId(attrId);
            attrAttrgroupRelationService.updateGroupId(attr);// 调用修改分组id 根据属性实体
            data.add(relationEntity);
        }

        return R.ok().put("data",data);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
}
